/*
** Copyright (C) 2009 Joshua Brent Marsh <joshua@icub3d.com>
**  
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 3 of the License, or
** (at your option) any later version.
**  
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**  
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
**  
*/

/* $Id: test_stack.c 44 2009-11-15 15:23:14Z joshua@icub3d.com $ */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>

#include <CUnit/Basic.h>

#include "../src/stack.h"

#include "test_helpers.h"
#include "test_stack.h"

Stack *stack;

int
init_test_stack_suite ()
{
  /* Create the stack we will be using for the tests. */
  if ((stack = stack_create ()) == NULL)
    return 1;
  else
    return 0;
}

int
clean_test_stack_suite ()
{
  /* Clean up the stack. */
  stack_destroy (stack, &destroy_int);

  return 0;
}

void
test_stack_push ()
{
  int *cur;

  /* Push a bunch of items on the stack and check the size change. */
  CU_ASSERT (stack_size (stack) == 0);
  cur = create_int (1);
  CU_ASSERT (stack_push (stack, cur) == 1);

  CU_ASSERT (stack_size (stack) == 1);
  cur = create_int (2);
  CU_ASSERT (stack_push (stack, cur) == 1);

  CU_ASSERT (stack_size (stack) == 2);
  cur = create_int (3);
  CU_ASSERT (stack_push (stack, cur) == 1);

  CU_ASSERT (stack_size (stack) == 3);
  cur = create_int (4);
  CU_ASSERT (stack_push (stack, cur) == 1);

  CU_ASSERT (stack_size (stack) == 4);

}

void 
test_stack_peek ()
{
  /* Test the peeks and make sure the size doesn't change. */
  CU_ASSERT (stack_size (stack) == 4);
  CU_ASSERT (*(int *)stack_peek (stack) == 4);
  CU_ASSERT (*(int *)stack_peek (stack) == 4);

  CU_ASSERT (stack_size (stack) == 4);
  CU_ASSERT (*(int *)stack_peek (stack) == 4);
  CU_ASSERT (*(int *)stack_peek (stack) == 4);
  CU_ASSERT (*(int *)stack_peek (stack) == 4);

  CU_ASSERT (stack_size (stack) == 4);
}

void
test_stack_pop ()
{
  int *cur;

  /* Remove a few items and verify the size. */
  CU_ASSERT (stack_size (stack) == 4);

  CU_ASSERT (* (int *)(cur = stack_pop (stack)) == 4);
  free (cur);
  CU_ASSERT (stack_size (stack) == 3);

  CU_ASSERT (* (int *)(cur = stack_pop (stack)) == 3);
  free (cur);
  CU_ASSERT (stack_size (stack) == 2);

}

void
test_stack_push2()
{
  int *cur;

  /* Do a few more pushes. */
  CU_ASSERT (stack_size (stack) == 2);
  cur = create_int (3);
  CU_ASSERT (stack_push (stack, cur) == 1);

  CU_ASSERT (stack_size (stack) == 3);
  cur = create_int (4);
  CU_ASSERT (stack_push (stack, cur) == 1);

  CU_ASSERT (stack_size (stack) == 4);
}

void
test_stack_pop2 ()
{
  int *cur;

  /* Do a few more pops. */
  CU_ASSERT (stack_size (stack) == 4);

  CU_ASSERT (* (int *)(cur = stack_pop (stack)) == 4);
  free (cur);
  CU_ASSERT (stack_size (stack) == 3);

  CU_ASSERT (* (int *)(cur = stack_pop (stack)) == 3);
  free (cur);
  CU_ASSERT (stack_size (stack) == 2);

}

int
make_stack_suite (CU_pSuite s)
{
  s = CU_add_suite ("Test Stack Suite", 
		    init_test_stack_suite, 
		    clean_test_stack_suite);
  if (s == NULL)
    {
      return CU_get_error();
    }

  if (CU_add_test (s, "test stack_push's", test_stack_push) == NULL ||
      CU_add_test (s, "test stack_peek's", test_stack_peek) == NULL ||
      CU_add_test (s, "test stack_pop's", test_stack_pop) == NULL ||
      CU_add_test (s, "test stack_push's 2", test_stack_push2) == NULL ||
      CU_add_test (s, "test stack_pop's 2", test_stack_pop2) == NULL
      )
    {
      return CU_get_error();
    }

  return CU_get_error();
}
